//
//  ZTListDetailViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ZTListDetailViewController.h"
#import "ZTListTrackTableViewCell.h"
#import "ZTListAthourTableViewCell.h"
#import "ZTListAlbumTableViewCell.h"
#import "ZTListTrackModel.h"
#import "ZTListAlbumModel.h"
#import "ZTListAuthorModel.h"
#import "AppTools.h"
#import "ZHPlayButtonView.h"
#import "ZHPlayViewController.h"
#import "ZTAlbumDetailViewController.h"
#import "ZHAnchorDetailViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ZTListDetailViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *listDetailArray;

@property (nonatomic, strong) UITableView *listDetailTableView;

@end

@implementation ZTListDetailViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTranslucent:NO];
    [[ZHPlayButtonView shareButton].musicBtn addTarget:self action:@selector(buttonActiont:) forControlEvents:UIControlEventTouchUpInside];
    [ZHPlayButtonView shareButton].frame =CGRectMake(WIDTH*0.4, HEIGHT-WIDTH*0.2-64, WIDTH*0.2, HEIGHT*0.2);
    [self.view addSubview :[ZHPlayButtonView shareButton]];
    if ([ZHPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePlaying) {
        
        
        [[ZHPlayButtonView shareButton] startCircle];
    }
    if ([ZHPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePaused) {
        
        [[ZHPlayButtonView shareButton] stopCircle];
        
    }
    
    [self.view bringSubviewToFront:self.messageView];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = self.listModel.title;
    
#pragma mark - 设置左按钮
    UIImage *leftImage = [UIImage imageNamed:@"icon_back_h.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    
#pragma mark - 设置UITableView    
    self.listDetailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 20 - 44) style:UITableViewStylePlain];
    [self.view addSubview:self.listDetailTableView];
    self.listDetailTableView.dataSource = self;
    self.listDetailTableView.delegate = self;
    self.listDetailTableView.rowHeight = 120;
    
    
    [self.listDetailTableView registerClass:[ZTListAlbumTableViewCell class] forCellReuseIdentifier:@"listAlbumTableCell"];
    [self.listDetailTableView registerClass:[ZTListAthourTableViewCell class] forCellReuseIdentifier:@"listAthourTableCell"];
    [self.listDetailTableView registerClass:[ZTListTrackTableViewCell class] forCellReuseIdentifier:@"listTrackTableCell"];
    
    self.listDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.listDetailArray = [NSMutableArray array];
    [self getData];
    
    
    
}

- (void)buttonActiont:(UIButton *)btn{
    
}
#pragma mark - 请求数据
- (void)getData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=iphone&key=%@&pageId=1&pageSize=40", self.contentType, self.key];
    NSLog(@"zh -  %@",urlStr);

    [AppTools getDataWithURL:urlStr Block:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            if ([self.contentType isEqualToString:@"track"]) {
               
                ZTListTrackModel *model = [ZTListTrackModel modelWithDictionary:dic];
                [self.listDetailArray addObject:model];
                
            }
            
            else if ([self.contentType isEqualToString:@"album"])
            {
                ZTListAlbumModel *model = [ZTListAlbumModel modelWithDictionary:dic];
                [self.listDetailArray addObject:model];
                
            }
            
            else {
                
                ZTListAuthorModel *model = [ZTListAuthorModel modelWithDictionary:dic];
                [self.listDetailArray addObject:model];
            }
        }
        [self.listDetailTableView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"error = %@", error);
        
    }];
    
}

#pragma mark - 左按钮 : 返回
- (void)leftButtonAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)backActionm:(UIButton *)btn{
    
    ZHPlayViewController *player = [ZHPlayViewController sharaMusicPlayer];
    ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc] initWithRootViewController:player];
    [self.view.window.rootViewController presentViewController:naVC animated:YES completion:^{
        
    }];
    
}

#pragma mark - tableView协议方法
// section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

// row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.listDetailArray.count;
}


// 创建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.contentType isEqualToString:@"track"])
    {
        ZTListTrackTableViewCell *trackCell = [tableView dequeueReusableCellWithIdentifier:@"listTrackTableCell"];
        
        ZTListTrackModel *model = [self.listDetailArray objectAtIndex:indexPath.row];
        
        trackCell.trackModel = model;
        trackCell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        
        return trackCell;
        
    }
    
    else if ([self.contentType isEqualToString:@"album"])
    {
        ZTListAlbumTableViewCell *albumCell = [tableView dequeueReusableCellWithIdentifier:@"listAlbumTableCell"];
        
        ZTListAlbumModel *model = [self.listDetailArray objectAtIndex:indexPath.row];
        
        albumCell.albumModel = model;
        albumCell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        
        return albumCell;
        
    }
    
    else {
        
        ZTListAthourTableViewCell *athourCell = [tableView dequeueReusableCellWithIdentifier:@"listAthourTableCell"];
        
        ZTListAuthorModel *model = [self.listDetailArray objectAtIndex:indexPath.row];
        athourCell.authorModel = model;
        athourCell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        
        return athourCell;
    }
}



#pragma mark - 点击跳转方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if ([self.contentType isEqualToString:@"track"])
    {
        ZTListTrackModel *model = [self.listDetailArray objectAtIndex:indexPath.row];
        ZHPlayViewController *player = [ZHPlayViewController sharaMusicPlayer];
        ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc] initWithRootViewController:player];
        
        player.song_id = model.trackId;
        player.allMusicArray = self.listDetailArray;
        player.indexRow = indexPath.row;
        [self presentViewController:naVC animated:YES completion:^{
            
        }];
    }
    else if ([_contentType isEqualToString:@"album"])
    {
        ZTListAlbumModel *model = [self.listDetailArray objectAtIndex:indexPath.row];
        
        ZTAlbumDetailViewController *albumsDetailVC = [[ZTAlbumDetailViewController alloc] init];
        
        albumsDetailVC.navigationController.navigationBar.translucent = YES;
        [(ZHTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
#warning mmmm
        albumsDetailVC.favorModel.albumId = model.albumId;
        albumsDetailVC.albumId = model.myId;
//                albumsDetailVC.myTitle = myTitle;
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
        
    }
    else
    {
        
        ZTListAuthorModel *model = [self.listDetailArray objectAtIndex:indexPath.row];
        ZHAnchorDetailViewController *authorDetailVC = [[ZHAnchorDetailViewController alloc] init];
        
        ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc] initWithRootViewController:authorDetailVC];
        
        authorDetailVC.uid = model.uid;
        //        authorDetailVC.myTitle = myTitle;
        [self.view.window.rootViewController presentViewController:naVC animated:YES completion:^{
            
            
        }];
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
